/*global pi, document, self */
// tooltip模块，tooltip实际是在root节点上的一个iframe，设置tooltip就是调用iframe中body.innerHTML。并自适应内容大小。
// tooltip会在以下几种情况下自动被清除， 1、所在forelet消失时。2、按键被按下时。3、鼠标划出tooltip所在元素的区域。
// 提示元素必须有tip_url属性，取tip_mix（或tip_func及tip_arg来计算mix）和页面来混合后显示信息（注意：tip_mix用字符串描述JSON对象，必须用双引号括起键和字符串，因此外层应该使用单引号描述这个字符串。例如：<div tip_url="../tooltip/tip.htm" tip_mix='{"tip":"aaaa"}'>区域</div>）
// 默认根据指针位置加偏移(属性为tip_x 默认0, tip_y 默认20)上显示提示，
// 提示的位置还可配置为上下左右(属性为tip_loc(值为0, top, topleft, topright, bottom, bottomleft, bottomright, left, lefttop, leftbottom, right, righttop, rightbottom), tip_x, tip_y)。
// 支持延时(属性为tip_delay)，如果需要更新tooltip需要forelet自己写函数完成。

pi.mod.define('pi.ui.tooltip', function(pi) {
	"use strict";
	// 定义模块
	var module = {},
	tooltip = {},
	count = 0,
	// 默认工具条提示位置
	TOOLTIP_Y = 20,

	// 显示tooltip，根据loc的值来决定显示的位置
	showTooltip = function(root, node, str, loc, x, y, x1, y1, x2, y2) {
		var width, height, win = pi.html.getIFrameWindow(node),
		doc = win.document,
		style = node.style,
		rootWidth = root.offsetWidth,
		rootHeight = root.offsetHeight;
		style.display = "none";
		doc.body.innerHTML = str;
		width = pi.html.contentWidth(doc);
		height = pi.html.contentHeight(doc);
		style.width = width + "px";
		style.height = height + "px";
		switch (loc) {
		case 0:
			break;
		case "bottom":
			y += y2;
			x += (x1 + x2 + width) / 2;
			break;
		case "bottomleft":
			y += y2;
			x += x1;
			break;
		case "bottomright":
			y += y2;
			x += x2 - width;
			break;
		case "top":
			y += y1;
			x += (x1 + x2 + width) / 2;
			break;
		case "topleft":
			y += y1;
			x += x1;
			break;
		case "topright":
			y += y1;
			x += x2 - width;
			break;
		case "left":
			x += x1 - width;
			y += (y1 + y2 + height) / 2;
			break;
		case "lefttop":
			x += x1 - width;
			y += y1;
			break;
		case "leftbottom":
			x += x1 - width;
			y += y2 - height;
			break;
		case "right":
			x += x2;
			y += (y1 + y2 + height) / 2;
			break;
		case "righttop":
			x += x2;
			y += y1;
			break;
		case "rightbottom":
			x += x2;
			y += y2 - height;
			break;
		default:
			break;
		}
		if (x < 0) {
			x = 0;
		} else if (x + width > rootWidth) {
			x = rootWidth - width;
		}
		if (y < 0) {
			y = 0;
		} else if (y + height > rootHeight) {
			y = rootHeight - height;
		}
		style.left = x + "px";
		style.top = y + "px";
		style.display = "";
	},
	// 设置tooltip，str是提示信息，为undefined时表示清除
	setTooltip = function(str, loc, x, y, x1, y1, x2, y2, info, delay, forelet) {
		var c = count++;
		tooltip.count = c;
		if (tooltip.delay) {
			pi.lang.clearTimeout(tooltip.delay);
		}
		tooltip.str = str;
		if (str) {
			tooltip.loc = loc;
			tooltip.x = x;
			tooltip.y = y;
			tooltip.x1 = x1;
			tooltip.y1 = y1;
			tooltip.x2 = x2;
			tooltip.y2 = y2;
			tooltip.info = info;
			tooltip.forelet = forelet;
			if (delay) {
				tooltip.delay = pi.lang.setTimeout(function() {
					if (c === tooltip.count) {
						showTooltip(tooltip.root, tooltip.node, str, loc, x, y, x1, y1, x2, y2);
					}
				},
				delay);
			} else {
				return showTooltip(tooltip.root, tooltip.node, str, loc, x, y, x1, y1, x2, y2);
			}
		}
		tooltip.node.style.display = "none";
	},
	// 按下时取消提示
	downHandler = function(e) {
		if (!tooltip.str) {
			return;
		}
		setTooltip();
	},
	// 移出范围取消提示
	moveHandler = function(e) {
		var x, y, forelet;
		if (!tooltip.str) {
			return;
		}
		x = e.xx;
		y = e.yy;
		forelet = e.forelet;
		if (forelet) {
			x += pi.html.offsetLeft(forelet.getNode(), tooltip.root);
			y += pi.html.offsetTop(forelet.getNode(), tooltip.root);
		}
		if (tooltip.x1 > x || tooltip.x2 < x || tooltip.y1 > y || tooltip.y2 < y) {
			setTooltip();
		}
	},
	// 显示提示
	overHandler = function(e) {
		var target, url, mix, loc, x, y, x1, y1, x2, y2, info, delay, forelet = e.forelet;
		if (!forelet) {
			return;
		}
		// 获取被提示对象
		target = pi.html.getElement(e.target, "tip_url");
		if (!target) {
			return;
		}
		url = target.getAttribute("tip_url");
		mix = target.getAttribute("tip_mix");
		if(!mix){
			mix = target.getAttribute("tip_func");
			if(!mix){
				return;
			}
			mix = forelet.exec(mix, [target.getAttribute("tip_arg"), target]);
		}
		loc = target.getAttribute("tip_loc") || 0;
		// 获得配置的提示偏移量
		x = target.getAttribute("tip_x");
		x = (x) ? pi.lang.parseInt(x) : 0;
		y = target.getAttribute("tip_y");
		y = (y) ? pi.lang.parseInt(y) : 0;
		// 获得forelet所在的位置
		x2 = pi.html.offsetLeft(forelet.getNode(), tooltip.root);
		y2 = pi.html.offsetTop(forelet.getNode(), tooltip.root);
		if (loc === 0) {
			x += e.xx + x2;
			y += e.yy + TOOLTIP_Y + y2;
		}
		//计算被提示对象的区域
		x1 = pi.html.offsetLeft(target) + x2;
		y1 = pi.html.offsetTop(target) + y2;
		x2 = x1 + target.offsetWidth;
		y2 = y1 + target.offsetHeight;
		// 如果基于指针位置提示，则提示不应该遮挡被提示对象
		if (loc === 0 && y2 > y) {
			y = y2;
		}
		info = target.getAttribute("tip_info");
		delay = target.getAttribute("tip_delay");
		pi.ui.page.load(pi.mod.absUrl(url, forelet.getUrl()), function(page) {
			var s, m;
			if (page.error) {
				s = "error:" + page.error + ", reason:" + page.reason;
			} else {
				m = mix ? JSON.parse(mix) : "";
				s = pi.ui.page.mixBody(page, m, true);
			}
			setTooltip(s, loc, x, y, x1, y1, x2, y2, info, delay ? pi.lang.parseInt(delay) : 0, forelet);
		});
	};

	// 初始化全局tooltip
	module.init = function(root) {
		var node;
		tooltip.root = root;
		pi.html.bind(root, "mousedown", downHandler);
		node = pi.html.createIframeNode(document);
		node.style.zIndex = 0x6fffffff;
		tooltip.node = node;
		node.onload = function() {
			node.onload = undefined;
			pi.html.bind(pi.html.getIFrameWindow(node).document.body, "mousemove", downHandler);
			pi.html.bind(pi.html.getIFrameWindow(node).document.body, "mousedown", downHandler);
			node = undefined;
		};
		root.appendChild(node);
	};
	// 获得全局tooltip的forelet
	module.getForelet = function() {
		return tooltip.forelet;
	};
	// 获得全局tooltip
	module.get = function() {
		return {
			str: tooltip.str,
			loc: tooltip.loc,
			x: tooltip.x,
			y: tooltip.y,
			x1: tooltip.x1,
			y1: tooltip.y1,
			x2: tooltip.x2,
			y2: tooltip.y2,
			info: tooltip.info,
			delay: tooltip.delay,
			forelet: tooltip.forelet
		};
	};
	// 设置全局tooltip
	module.set = setTooltip;
	// 创建tooltip事件处理器
	module.bind = function(handlerTable) {
		handlerTable.mousedown.addPre(downHandler);
		handlerTable.mousemove.addPre(moveHandler);
		handlerTable.mouseover.add(overHandler);
	};

	return module;
});